<template>
  <div class="contentpage">
    <!-- v-bind里面有propList,但有错误,后期解决 -->
    <FhTable
      v-bind="contentTableConfig"
      :propList="contentTableConfig.propList"
      :dataList="datalist"
      :dataCount="dataCount"
      @SelectionChange="SelectionChange"
      v-model:page="PageInfo"
    >
      <template #headerHandler>
        <el-button
          v-if="isCreatePermissions"
          :icon="EditPen"
          type="primary"
          @click="insertNewClick"
        >
          新建数据
        </el-button>
      </template>
      <!-- 列中插槽替换 -->
      <template #enable="xxx">
        <!-- 替换table.vue里面绑定propList对象的prop的插槽 -->
        <el-button
          size="small"
          plain
          :type="xxx.row.enable ? 'success' : 'danger'"
        >
          {{ xxx.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>
      <template #status="xxx">
        <!-- 替换table.vue里面绑定propList对象的prop的插槽 -->
        <el-button
          size="small"
          plain
          :type="xxx.row.status ? 'success' : 'danger'"
        >
          {{ xxx.row.status ? '启用' : '禁用' }}
        </el-button>
      </template>
      <template #createAt="xxx">
        <!-- 替换table.vue里面绑定propList对象的prop的插槽 -->
        {{ $filters.formatTime(xxx.row.createAt) }}
      </template>
      <template #updateAt="xxx">
        {{ $filters.formatTime(xxx.row.updateAt) }}
      </template>
      <template #handler="xxx">
        <div>
          <el-button
            v-if="isUpdatePermissions"
            size="small"
            type="text"
            :icon="Edit"
            @click="EditClick(xxx.row)"
          >
            编辑
          </el-button>
          <el-button
            v-if="isDeletePermissions"
            size="small"
            type="text"
            :icon="Delete"
            @click="deleteClick(xxx.row)"
            >删除</el-button
          >
        </div>
      </template>
      <!-- 插入剩余的插槽 -->
      <template v-for="item in otherSlots" :key="item.prop" #[item.prop]="xxx">
        <slot :name="item.prop" :row="xxx.row"></slot>
      </template>
    </FhTable>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import { useStore } from '@/store'
import { Edit, Delete, User, EditPen } from '@element-plus/icons-vue'
import FhTable from '@/base-ui/table'
import { UsePermission } from '@/hooks/UsePermission'
export default defineComponent({
  emits: ['SelectionChange', 'insertNewClick', 'EditClick'],
  components: {
    FhTable
  },
  props: {
    contentTableConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  setup(props, { emit }) {
    const store = useStore()

    // 对权限进行处理
    const isCreatePermissions = UsePermission(props.pageName, 'create')
    const isUpdatePermissions = UsePermission(props.pageName, 'update')
    const isDeletePermissions = UsePermission(props.pageName, 'delete')
    // const isQueryPermissions = UsePermission(props.pageName, 'query')

    // 双向绑定分页器的信息
    //                     当前页数         每页显示条目个数
    const PageInfo = ref({ currentPage: 1, pageSize: 10 })

    watch(PageInfo, () => {
      getPageData()
    })

    // 发送网络请求
    const getPageData = (queryInfo: any = {}) => {
      // if (!isQueryPermissions) return
      store.dispatch('system/getPageListAtion', {
        pageName: props.pageName,
        queryInfo: {
          offset: (PageInfo.value.currentPage - 1) * PageInfo.value.pageSize,
          size: PageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()

    // 从vuex中获取数据
    const datalist = computed(() =>
      store.getters[`system/PageListData`](props.pageName)
    )

    const dataCount = computed(() =>
      store.getters['system/PageListCount'](props.pageName)
    )

    const SelectionChange = (value: any[]) => {
      emit('SelectionChange', value)
    }

    // 获取其他的动态插槽名称
    const otherSlots = props.contentTableConfig.propList.filter((item: any) => {
      if (item.prop === 'enable') return false
      if (item.prop === 'status') return false
      if (item.prop === 'createAt') return false
      if (item.prop === 'updateAt') return false
      if (item.prop === 'handler') return false
      return true
    })

    // 删除的按钮的处理
    const deleteClick = (val: any) => {
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        id: val.id
      })
    }
    // 新建用户按钮的点击
    const insertNewClick = () => {
      emit('insertNewClick')
    }

    // 编辑按钮的点击
    const EditClick = (item: any) => {
      emit('EditClick', item)
    }

    return {
      Edit,
      Delete,
      User,
      EditPen,
      SelectionChange,
      datalist,
      dataCount,
      getPageData,
      PageInfo,
      otherSlots,
      isCreatePermissions,
      isUpdatePermissions,
      isDeletePermissions,
      deleteClick,
      insertNewClick,
      EditClick
    }
  }
})
</script>

<style scoped lang="less"></style>
